﻿Imports System.IO

Public Class frmPoliciesDocuments
    Public TransNo As Integer

    Sub FillDocuments()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim Dir As New IO.DirectoryInfo(FTPAddress & "Production Corporate\" & TransNo)
            Dim DirFiles As IO.FileInfo() = Dir.GetFiles()

            Me.GridFiles.Rows.Clear()

            'list the names of all files in the specified directory
            For Each Fi As FileInfo In DirFiles
                Me.GridFiles.Rows.Add(New String() {Fi.Name, Fi.CreationTime.ToString("yyyy/MM/dd hh:mm tt"), "Open", "Delete"})
            Next

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub frmPoliciesDocuments_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillDocuments()
    End Sub

    Private Sub GridFiles_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridFiles.CellClick
        If e.ColumnIndex = Me.DGColDocOpen.Index Then
            'Dim Fi As New FileInfo(FTPAddress + "Claims\" + ClaimNo + "\" + Me.GridFiles.Rows(e.RowIndex).Cells(0).Value)
            'Fi.Open(FileMode.Open, FileAccess.ReadWrite)
            Try
                Me.Cursor = Cursors.WaitCursor

                Dim FilePath As String = FTPAddress & "Production Corporate\" & TransNo & "\" & Me.GridFiles.Rows(e.RowIndex).Cells("DGColFile").Value
                System.Diagnostics.Process.Start(FilePath)

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Private Sub btnAddDocument_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddDocument.Click
        Try
            Me.OpenFileDialog1.Filter = "All files (*.*)|*.*"
            If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

                'Set files
                Dim SelFile As FileInfo
                Dim SelFilePath, DestFilePath As String

                SelFilePath = Me.OpenFileDialog1.FileName
                SelFile = New FileInfo(SelFilePath)
                DestFilePath = FTPAddress & "Production Corporate\" & TransNo & "\" & SelFile.Name

                My.Computer.Network.UploadFile(SelFilePath, DestFilePath, FTPUserName, FTPPassword, True, 500)

                FillDocuments()
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class